<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/26
 * Time: 13:23
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
use Qiniu\Auth;// 引入鉴权类
use Qiniu\Storage\UploadManager;// 引入上传类
use Qiniu\Processing\PersistentFop;
require_once('../extend/qiniu/autoload.php');
class Music extends Base{
    public function index(){
        if (request()->isAjax()) {
            extract(input());
            $map =array();
            if(isset($keyword)&&$keyword!="")
            {
                $map['music_name'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('music')->alias('a')
                ->join('up_music_type uvt','uvt.music_type_id = a.f_id','left')
                ->join('up_music_modular uvm','uvm.video_modular_id = a.music_f_id','left')
                ->join('up_school us','us.hot_id = a.music_lp','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('music')->alias('a')
                ->join('up_music_type uvt','uvt.music_type_id = a.f_id','left')
                ->join('up_music_modular uvm','uvm.video_modular_id = a.music_f_id','left')
                ->join('up_school us','us.hot_id = a.music_lp','left')
                ->page($NowPage, $limits)
                ->field('us.hot_keyword,a.*,uvt.music_type_name,uvm.video_modular_name')
                ->where($map)
                ->order('a.music_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $personList = explode(',',trim($vo['person_id'],','));
                $person_name = Db::table('up_person')->where('person_id','in',$personList)->column('person_name');
                $vo['person_name'] = implode(',',$person_name);
            }
            foreach ($lists as $key=>&$vo){
                if($vo['is_down']==1){
                    $vo['is_down_name'] = '可以下载';
                }else{
                    $vo['is_down_name'] = '不可以下载';
                }
                if($vo['is_vip']==1){
                    $vo['is_vip_name'] = '普通';
                }else{
                    $vo['is_vip_name'] = 'Vip';
                }

                if($vo['is_index']==1){
                    $vo['is_index_name'] = '是';
                }else{
                    $vo['is_index_name'] = '否';
                }
                if($vo['is_new']==1){
                    $vo['is_new_name'] = '是';
                }else{
                    $vo['is_new_name'] = '否';
                }
                if($vo['is_listen']==1){
                    $vo['is_listen_name'] = '是';
                }else{
                    $vo['is_listen_name'] = '否';
                }
                if($vo['is_sxzlx']==1){
                    $vo['is_sxzlx_name'] = '是';
                }else{
                    $vo['is_sxzlx_name'] = '否';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?video_id2')){
            session('video_id2',null);
        }
        return $this->fetch();
    }

    public function add(){
        if(Request::instance()->isPost()){
            $video_name = input('param.video_name');
            $f_id = input('param.f_id');
            $video_f_id = input('param.video_f_id');
            $person_id = input('param.person_id');
            $video_performer = input('param.video_performer');
            $is_down = input('param.is_down');
            $is_vip = input('param.is_vip');
            $video_lp= input('param.video_lp');
            $video_year = input('param.video_year');
            $video_img = input('param.video_img');
            $video_intro= input('param.video_intro');
            $content= input('param.content');
            $person_troupe= input('person_troupe');
            $data['person_troupe'] = $person_troupe;
            $data['music_name'] = $video_name;
            $data['f_id'] = $f_id;
            $data['music_f_id'] = $video_f_id;
            $data['person_id'] = ','.$person_id.',';
            $data['music_performer'] = $video_performer;
            $data['is_down'] = $is_down;
            $data['is_vip'] = $is_vip;
            $data['music_lp'] = $video_lp;
            $data['music_year'] = $video_year;
            $data['music_img'] = $video_img;
            $data['music_intro'] = $video_intro;
            $data['content'] = $content;
            $data['music_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_music')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info = Db::table('up_music_type')->select();
        $info1 = Db::table('up_music_modular')->select();
        $info2 = Db::table('up_person')->select();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        $this->assign('info2',$info2);
        $info3 = Db::table('up_school')->select();
        $this->assign('info3',$info3);
        $info4 = Db::table('up_troupe_type')->select();
        $this->assign('info4',$info4);
        return $this->fetch();
    }

    public function update(){
        if(Request::instance()->isPost()){


            $id = input('param.id');
            $video_name = input('param.video_name');
            $f_id = input('param.f_id');
            $video_f_id = input('param.video_f_id');
            $person_id = input('param.person_id');
            $video_performer = input('param.video_performer');

            $video_lp= input('param.video_lp');
            $video_year = input('param.video_year');
            $video_img = input('param.video_img');
            $video_intro= input('param.video_intro');
            $content= input('param.content');
            $person_troupe= input('person_troupe');
            $data['person_troupe'] = $person_troupe;
            $data['content'] = $content;
            $data['music_name'] = $video_name;
            $data['f_id'] = $f_id;
            $data['music_f_id'] = $video_f_id;
            $data['person_id'] = ','.$person_id.',';
            $data['music_performer'] = $video_performer;

            $data['music_lp'] = $video_lp;
            $data['music_year'] = $video_year;
            $data['music_intro'] = $video_intro;
            if(!empty($video_img)){
                $data['music_img'] = $video_img;
            }
            $res = Db::table('up_music')->where('music_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_music')->where('music_id',$id)->find();
        $personList = explode(',',trim($res['person_id'],','));
        $this->assign('personList',$personList);

        $info = Db::table('up_music_type')->select();
        $info1 = Db::table('up_music_modular')->select();
        $info2 = Db::table('up_person')->select();
        $this->assign('id',$id);
        $this->assign('res',$res);
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        $this->assign('info2',$info2);
        $info3 = Db::table('up_school')->select();
        $this->assign('info3',$info3);
        $info4 = Db::table('up_troupe_type')->select();
        $this->assign('info4',$info4);
        return $this->fetch();
    }

    public function isIndex(){
        $task_id = input('param.id');
        $num = input('param.num');
        if($num==1){
            $count = Db::table('up_music')
                ->where('is_index',1)
                ->count();
            if($count>=3){
                return json(['code'=>100,'msg'=>'最多指定3个，请先下架其他在指定']);
            }
        }

        $res = Db::name('music')->where('music_id',$task_id)->setField('is_index',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function isNew(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_new',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function isListen(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_listen',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function isSxzlx(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_sxzlx',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function isType(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_type',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    public function isDown(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_down',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    public function isVip(){
        $task_id = input('param.id');
        $num = input('param.num');
        $res = Db::name('music')->where('music_id',$task_id)->setField('is_vip',$num);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    public function index1(){
        if (request()->isAjax()) {
            extract(input());
            $where['f_id'] = input('param.id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('music_chirden')->alias('a')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('music_chirden')->alias('a')
                ->page($NowPage, $limits)
                ->where($where)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?video_id2')){
            session('video_id2',input('id'));
        }
        return $this->fetch();
    }

    public function add1(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传音频']);
            }
            //转码的文件url
            $url = "http://xihaiqiniu.sooqee.cn/" . $params['video'];//未转码之前的视频url，用于获取时长
            //时长
            $a = file_get_contents($url . '?avinfo');
            $b = json_decode($a,true);
            $time = explode('.', $b['streams'][0]['duration']);
            $fen = floor($time[0]/60);
            $miao = floor(($time[0]-$fen*60)%60);
//            $this->zhuanma($params['video']);
            $data = [
                'chirden_title' => $params['chirden_title'],//标题
                'chirden_url' => $url,//视频
                'f_id' => session('video_id2'),//视频ID
                'chirden_time' => date('Y-m-d H:i:s'),//创建时间
                'catalogue_time' => $fen . ':' . $miao//视频时长
            ];
            $res = Db::table('up_music_chirden')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    public function update1(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['video'])){
                return json(['code' => 100, 'msg' => '请上传音频']);
            }
            $old_url = Db::table('up_music_chirden')->where('chirden_id', $params['id'])->value('chirden_url');
            //转码的文件url
            //有视频但是和原视频地址不同
            if($params['video'] != '' && $params['video'] != $old_url){
                $url = "http://xihaiqiniu.sooqee.cn/" . $params['video'];//未转码之前的视频url，用于获取时长
                //时长
                $a = file_get_contents($url . '?avinfo');
                $b = json_decode($a,true);
                $time = explode('.', $b['streams'][0]['duration']);
                $fen = floor($time[0]/60);
                $miao = floor(($time[0]-$fen*60)%60);
//                $this->zhuanma($params['video']);
                $data['chirden_url'] = $url;//视频地址
                $data['chirden_time'] = $fen . ':' . $miao;//视频时长
            }
            $data['chirden_title'] = $params['chirden_title'];
            $res = Db::table('up_music_chirden')->where('chirden_id',$params['id'])->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_music_chirden')->where('chirden_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$res);
        return $this->fetch();
    }


    /***
     * @return \think\response\Json
     *
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('music')->where('music_id',$task_id)->delete();
        $list = Db::name('music_chirden')->where('f_id',$task_id)->find();
        if($list['chirden_id']!=''){
            $res = Db::name('music_chirden')->where('f_id',$task_id)->delete();
        }
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }

    }

    public function del1(){
        $task_id = input('param.id');
        $res = Db::name('music_chirden')->where('chirden_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}